package sk.kainitram.fuel.db;

import java.util.ArrayList;
import java.util.List;

import com.kainitram.morm.Session;
import com.kainitram.morm.SessionBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import sk.kainitram.fuel.model.*;

public class CarContract {

	private static CarContract _instance;

	public static abstract class CarEntry implements BaseColumns {
		public static final String TABLE_NAME = "Car";
		public static final String COLUMN_NAME_SPZ = "Spz";
	}

	private static final String TEXT_TYPE = " TEXT";
	private static final String COMMA_SEP = ",";
	private static final String SQL_CREATE_ENTRIES = "CREATE TABLE "
			+ CarContract.CarEntry.TABLE_NAME + " (" + CarEntry._ID
			+ " INTEGER PRIMARY KEY," + CarEntry.COLUMN_NAME_SPZ + TEXT_TYPE
			+ " )";

	private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS "
			+ CarEntry.TABLE_NAME;

	private CarContract() {
	}

	public static CarContract getInstance() {
		if (_instance == null) {
			_instance = new CarContract();
		}

		return _instance;
	}

	public List<Car> GetAllCars(Context context) {
		ArrayList<Car> cars = new ArrayList<Car>();
		CarDBHelper dbHelper = new CarDBHelper(context);

		Session session = new SessionBase();
		SQLiteDatabase db = dbHelper.getReadableDatabase();

		// Define a projection that specifies which columns from the database
		// you will actually use after this query.
		String[] projection = { CarContract.CarEntry._ID,
				CarContract.CarEntry.COLUMN_NAME_SPZ };

		// How you want the results sorted in the resulting Cursor
		String sortOrder = CarContract.CarEntry.COLUMN_NAME_SPZ + " DESC";

		Cursor c = db.query(CarContract.CarEntry.TABLE_NAME, // The table to
																// query
				projection, // The columns to return
				null, // The columns for the WHERE clause
				null, // The values for the WHERE clause
				null, // don't group the rows
				null, // don't filter by row groups
				sortOrder // The sort order
				);

		while (c.moveToNext()) {
			String spz = c
					.getString(c
							.getColumnIndexOrThrow(CarContract.CarEntry.COLUMN_NAME_SPZ));
			int id = c
					.getInt(c
							.getColumnIndexOrThrow(CarContract.CarEntry._ID));
			Car car = new Car(session);
			car.setID(id);
			car.setSpz(spz);			
			cars.add(car);
		}
		
		c.close();
		db.close();
		
		return cars;
	}

	public void InsertCar(Car car, Context context) {
		CarDBHelper dbHelper = new CarDBHelper(context);

		SQLiteDatabase db = dbHelper.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(CarContract.CarEntry.COLUMN_NAME_SPZ, car.getSpz());

		// Insert the new row, returning the primary key value of the new row
		long newRowId;
		newRowId = db.insert(CarContract.CarEntry.TABLE_NAME, null, values);
		
		car.setID(newRowId);
		
		db.close();
	}
	
	
	public void DeleteCar(Car car, Context context) {
		CarDBHelper dbHelper = new CarDBHelper(context);		

		SQLiteDatabase db = dbHelper.getWritableDatabase();

		String[] whereArgs = { String.valueOf(car.getID()) };
		db.delete(CarContract.CarEntry.TABLE_NAME, CarContract.CarEntry._ID
				+ " = ?", whereArgs);
		
		db.close();
	}

	public class CarDBHelper extends SQLiteOpenHelper {

		public static final int DATABASE_VERSION = 1;
		public static final String DATABASE_NAME = "Fuel.db";

		public CarDBHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(SQL_CREATE_ENTRIES);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL(SQL_DELETE_ENTRIES);
			onCreate(db);
		}

		public void onDowngrade(SQLiteDatabase db, int oldVersion,
				int newVersion) {
			onUpgrade(db, oldVersion, newVersion);
		}

	}
}
